<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Timesheet extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
	private $timesheetModel;
	private $requestManager;
    public function __construct()
    {
        parent::__construct();
        
   	 	if(isset($this->session) && !$this->session->userdata('isLogged')) {
        	redirect('/');
        }
        
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->timesheetModel = $this->load->model('Timesheet_Model');
        $this->settings = $this->config->item('settings');
        $this->requestManager = RequestManager::getInstance();
        $this->load->helper(array('ConstantsUtils','StringUtils','DateUtils'));
    }
    

	
	public function summary_period($payroll_period_id){
		//$this->load->helper(array('ConstantsUtils','StringUtils','DateUtils'));
		$data = array();
		$data['start_time'] = CommonUtils::getCurrentTime();
		$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/summary_period/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['message'] = $return->message;
			$data['timesheet'] = $return->data->all;
			$data['absences'] = $return->data->absences;
			$data['leave_with_pay'] = $return->data->leave_with_pay;
			$data['leave_without_pay'] = $return->data->leave_without_pay;
			$data['wrong_card'] = $return->data->wrong_card;
			$data['period'] = $return->data->period;
		}
		
		// get all payroll periods
		$this->requestManager->setData(array());
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/view/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		
		if($return->message == 'success'){
			$data['periods'] = $return->data;
		}
		
		$this->load->view('header');
		$this->load->view('summary',$data);
		$this->load->view('footer');
	}
	
	public function view_user($payroll_period_id, $user_id){
		//$this->load->helper(array('ConstantsUtils','StringUtils','DateUtils'));
		$data = array();
		$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id, 'user_id'=>$user_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/view_user/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		if($return->message == 'success'){
			$data['message'] = $return->message;
			$data['timesheet'] = $return->data;
			$data['period'] = $return->period;
			$data['user_id'] = $user_id;
			$data['rest_day'] = $return->rest_day;
			$data['timesheet_payroll'] = $return->timesheet_payroll;
			//var_dump($return->rest_day);
		}
		
		$this->load->view('header');
		$this->load->view('view_user',$data);
		$this->load->view('footer');
	}
	
	public function details($user_id, $timesheet_id){
		$data = array();
		$this->requestManager->setData(array('user_id'=>$user_id, 'timesheet_id'=>$timesheet_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/details/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		if($return != NULL) {
			if($return->message == 'success'){
				$data['message'] = $return->message;
				$data['timesheet'] = $return->data;
				$data['extra_timesheet'] = $return->extra_timesheet;
				$data['rest_day'] = $return->rest_day;
			}
		}
		//var_dump($return->extra_timesheet);
		$this->load->view('lightbox_header');
		$this->load->view('timesheet_details',$data);
		$this->load->view('lightbox_footer');
	}
	
	public function view($payroll_period_id=0){
		$data = array();
		$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/view/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		if($return->message == 'success'){
			$data['message'] = $return->message;
			$data['periods'] = $return->data;
			//$data['rest_day'] = $return->rest_day;
		}
		
		$this->load->view('header');
		$this->load->view('index',$data);
		$this->load->view('footer');
	}
	
	public function update(){
		$data = array();
		if($this->input->post()){
			$date = $this->input->post('date');
			$params['time_in'] = $date ." ".$this->input->post('time_in');
			$params['time_out'] = $date ." ".$this->input->post('time_out');
			$params['late'] = $this->input->post('late');
			$params['earlier'] = $this->input->post('earlier');
			$params['overtime'] = $this->input->post('overtime');
			$params['approved_overtime'] = $this->input->post('approved_overtime');
			$params['undertime'] = $this->input->post('undertime');
			$params['approved_undertime'] = $this->input->post('approved_undertime');
			$params['night_differential'] = $this->input->post('night_differential');
			$params['approved_night_differential'] = $this->input->post('approved_night_differential');
			$params['remarks'] = $this->input->post('remarks');
			$params['is_saved'] = 1;
			$params['id'] = $this->input->post('id');
			//var_dump($params);
			
			$this->requestManager->setData($params);
			$this->requestManager->setSendType("post");
			$this->requestManager->setAccessPath($this->settings['api_path']."timesheet/update/" .$this->settings['client_key']);
			$return = $this->requestManager->send();
			//var_dump($return);
			
			if($return->message == 'success'){
				$data['message'] = $return->message;
				$data['updated'] = true;
				//$data['periods'] = $return->data;
			}
			$data['periods'] = $return->data;
			$this->load->view('lightbox_header');
			$this->load->view('timesheet_details',$data);
			$this->load->view('lightbox_footer');
		}
	}
	
}